package com.ssmtest.controller;

import com.alibaba.fastjson.JSONObject;
import com.ssmtest.entity.Student;
import com.ssmtest.entity.Teacher;
import com.ssmtest.service.StudentService;
import com.ssmtest.service.TeacherService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class teacherController {
    @Resource
    private TeacherService teacherService;
    @Resource
    private StudentService studentService;


//学生
    @RequestMapping("/examlogin")
     public String examlogin(){
        return "examlogin";
    }
    @RequestMapping("/exam")
    public String exam(@RequestParam("username")String username,
                        @RequestParam("password")String password, @RequestParam("role")String role, HttpSession session) throws Exception {

/*       if ("teacher".equals(role)) {
            // 如果是学生身份登录的话,判断账号密码正确与否
            if (teacherService.allowLogin(username, password)) {
                Teacher studentInfo = teacherService.getTeacherInfo(username);
                session.setAttribute("teacherInfo", studentInfo);
                return "teacher";
            } else {
                return "error";
            }
        }else{
           return "edit";
       }*/
        if("teacher".equals(role)){
            Teacher teacher =teacherService.allowLogin(username,password);
            session.setAttribute("teacherinfo",teacher);
            if(teacher!=null){
                return "teacher";
            }else {
                return "error";
            }
        }else {
            Student student=studentService.allowLogin(username,password);
            session.setAttribute("studentinfo",student);
            if(student!=null){
                return "student";

            }else{
                return "error";
            }
        }

}
    @RequestMapping("/studentadd")
    public String studentadd(){
        return "studentAdd";
    }
    @RequestMapping("/teacher")
    public String teacher(){
        return "teacher";
    }
    @RequestMapping("/studentinfoadd")
    public String studentinfoadd(String studentID, String studentName, String sclass, HttpServletResponse response) throws IOException {
              JSONObject json = new JSONObject();
              studentService.insertSelective(studentID,studentName,sclass);
              json.put("res","1");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json.toString());
            return "studentAdd";
    }
    @RequestMapping("/studentQuery")
    public String  studentQuery(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage, Model model){
        model.addAttribute("pagemsg", studentService.findByPage(currentPage));//回显分页数据

        return "studentManager";
    }
    @RequestMapping("/selectbyclass")
    public String selectbyclass(String sclass,Model model){
        List<Student> student=studentService.selectbyclass(sclass);
        model.addAttribute("sclass",student);

        return "studentQueryByClass";
    }

    @RequestMapping("/sclass")
    public String sclass(){

        return "studentQueryByClass";
    }
    @RequestMapping("/name")
    public String name(){

        return "studentQueryByName";
    }
    @RequestMapping("/selectbyname")
    public String selectbyname(String studentName,Model model){
        List<Student> student=studentService.selectbyname(studentName);
        model.addAttribute("name",student);
        return "studentQueryByName";
    }
    @RequestMapping("/studentid")
    public String studentid(){

        return "studentQueryByID";
    }
    @RequestMapping("/selectbyid")
    public String selectbyid(String studentID,Model model){
        List<Student> student=studentService.selectbyid(studentID);
        model.addAttribute("id",student);
        return "studentQueryByID";
    }
    //考试试题
      //试题编辑
    @RequestMapping("/xuanzesubjectedit")
    public String xuanzesubjectedit(){
        return "xuanzesubjectedit";
    }
       //选择题
    @RequestMapping("/xuanzesubjectadd")
    public String xuanzesubjectadd(){
        return "subjectAdd";
    }
    @RequestMapping("/xuanzesubject")
    public String xuanzesubject(String stTitle,String stOptionA,String stOptionB,String stOptionC,String stOptionD,String stAnswer,String stParse){
        studentService.insertxuanzesubject(stTitle,stOptionA,stOptionB,stOptionC,stOptionD,stAnswer,stParse);
        return "subjectAdd";
    }
    @RequestMapping("/selectxuanze")
    public String  selectxuanze(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage, Model model){
        model.addAttribute("pagemsg1", studentService.findByPagexuanze(currentPage));//回显分页数据
        return "subjectLikeManager";
    }
       //填空题
    @RequestMapping("/tiankongsubjectadd")
    public String tiankongsubjectadd(){

        return "tiankongsubjectAdd";
    }
    @RequestMapping("/tiankongsubject")
    public String tiankongsubject(String stTitle,String stAnswer,String stParse){
        studentService.inserttiankongsubject(stTitle,stAnswer,stParse);
        return "tiankongsubjectAdd";
    }
    //判断题
    @RequestMapping("/panduansubjectadd")
    public String panduansubjectadd(){
        return "panduansubjectAdd";
    }
    @RequestMapping("/panduansubject")
    public String panduansubject(String stTitle,String stAnswer,String stParse){
        studentService.insertpanduansubject(stTitle,stAnswer,stParse);
        return "panduansubjectAdd";
    }
    //问答题
    @RequestMapping("/wendasubjectadd")
    public String wendasubjectadd(){
        return "wendasubjectAdd";
    }
    @RequestMapping("/wendasubject")
    public String wendasubject(String stTitle,String stAnswer,String stParse){
        studentService.insertwendasubject(stTitle,stAnswer,stParse);
        return "wendasubjectAdd";
    }
    @RequestMapping("/querysubject")
    public String querysubject(){
        return "subjectQuery";
    }
    @RequestMapping("/selectsubject")
    public String selectsubject(){
        return "subjectManager";
    }

}
